package com.ls.fw.imgserver.controller;


import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ls.fw.web.core.servlet.bean.ProgressInfo;
import com.ls.fw.web.core.servlet.bean.ValidResult;
import com.ls.fw.web.file.upload.BaseUpload;


public class FileUpload extends BaseUpload {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void illegalRequest(ValidResult validResult, HttpServletRequest request, HttpServletResponse response, Object... paras) {
		 super.illegalRequest(validResult, request, response, paras);
	}

	@Override
	protected void callback(HttpServletRequest request, HttpServletResponse response,
			Object... paras) {
		super.callback(request, response,paras);
	}

	@Override
	protected void exceptionCaught(HttpServletRequest request,
			HttpServletResponse response, Object msg, Throwable throwable) {
		super.exceptionCaught(request, response, msg, throwable);
	}

	@Override
	public String getSaveRelativeDir(HttpServletRequest request) {
		return super.getSaveRelativeDir(request);
	}
	
	@Override
	public void progress(HttpServletResponse response,double megaBytes,final long unit, long pBytesRead,
			long pContentLength, int pItems) {
		
	    double mBytes = pBytesRead / (unit*1.0);
	    BigDecimal b= new BigDecimal(mBytes);
	    mBytes =  b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        if (megaBytes == mBytes) {
           return;
        }
        megaBytes = mBytes;
        if (pContentLength == -1) {
        	 this.log("So far, " + pBytesRead + " bytes have been read.");
        	 return;
        }
	   	double percent = ((pBytesRead) / (pContentLength*1.0)) * 100;
    	BigDecimal big = new BigDecimal(percent);
		double f1 =  big.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        ProgressInfo p = new ProgressInfo();
        p.setItem(pItems);
        p.setPercent(f1);;
        p.setReaded(pBytesRead);
        p.setTotal(pContentLength);
        System.out.println(p);
        this.sendJSONObject(response, "progressInfo", p);
     }
}
